using System;
using System.Collections.Generic;
using System.Text;

namespace Fuzzy
{
    /// <summary>
    /// A class representing a Mamdani fuzzy controller.
    /// </summary>
    public class MamdaniFuzzyController:FuzzyController
    {

        /// <summary>
        /// Calculates the output of the fuzzy controller.
        /// </summary>
        /// <param name="input">The input fuzzy sets.</param>
        /// <returns>The output fuzzy set.</returns>
        public override FuzzySet GetOutput(FuzzySet[] input)
        {
            List<FuzzySet> outputSets = new List<FuzzySet>();

            FuzzySet[] currentAntecedents;
            double minSup;
            for (int i = 0; i < fuzzyRules.Count; i++)
            {
                currentAntecedents = fuzzyRules[i].Antecedents;
                minSup = currentAntecedents[0].Intersection(input[0]).Sup();
                for (int j = 1; j < input.Length; j++)
                {
                    double currentSup = currentAntecedents[j].Intersection(input[j]).Sup();
                    if (currentSup < minSup)
                        minSup = currentSup;
                }
                outputSets.Add(new ConstantValueFuzzySet(
                            fuzzyRules[i].Consequent.FuzzyLinguisticVariable, minSup
                        ).Intersection(fuzzyRules[i].Consequent));
            }
            FuzzySet output = outputSets[0];

            for (int i = 1; i < outputSets.Count; i++)
            {
                output = output.Union(outputSets[i]);
            }

            return output;
        }
    }
}
